package com.gmail.gwanndan.common.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Dennis Ahaus, dennis.ahaus@gmail.com
 * @version 1.0
 * 
 * 
 */
public class HashUtil {

	/**
	 * Hash a String to MD5 Hash
	 * 
	 * @param data
	 *            the data to hash
	 * @return the hash-string
	 * @throws NoSuchAlgorithmException
	 *             this exception is thrown when there is no MD5 algorithm in
	 *             {@link MessageDigest}
	 */
	public static String toMD5(String data) throws NoSuchAlgorithmException {
		MessageDigest m = MessageDigest.getInstance("MD5");
		byte[] bytes = data.getBytes();
		m.update(bytes, 0, bytes.length);
		BigInteger i = new BigInteger(1, m.digest());
		return String.format("%1$032X", i);
	}

}
